// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "fault_description": {
    "rules": [
      {
        "required": true,
        "errorMessage": "故障描述不能为空"
      },
      {
        "format": "string"
      },
      {
        "minLength": 5,
        "maxLength": 30,
        "errorMessage": "故障描述不能小于{minLength}个字符"
      }
    ],
    "label": "故障描述",
    "title": "故障描述"
  },
  "fault_code": {
    "rules": [
      {
        "format": "string"
      },
      {
        "maxLength": 20,
        "errorMessage": "故障代码不能大于{maxLength}个字符"
      },
      {
        "pattern": "^[A-Z0-9-]+$",
        "errorMessage": "故障代码只允许输入大写字母A-Z,数字0-9和‘-’"
      }
    ],
    "label": "故障代码",
    "title": "故障代码"
  },
  "brand": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "label": "品牌",
    "title": "品牌"
  },
  "model": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "label": "型号",
    "title": "型号"
  },
  "fault_information": {
    "rules": [
      {
        "required": true,
        "errorMessage": ""
      },
      {
        "format": "string"
      },
      {
        "maxLength": 200,
        "errorMessage": "{title}不能大于{maxLength}个字符"
      }
    ],
    "label": "详细信息",
    "title": "详细信息"
  },
  "fault_photo": {
    "rules": [
      {
        "format": "array"
      },
      {
        "arrayType": "file"
      },
      {
        "maxLength": 10
      }
    ],
    "label": "故障照片",
    "title": "故障照片"
  },
  "is_lock": {
    "rules": [
      {
        "format": "bool"
      }
    ],
    "label": "锁定",
    "title": "锁定",
    "defaultValue": false
  },
  "updata_time": {
    "rules": [
      {
        "format": "timestamp"
      }
    ],
    "label": "更新时间",
    "title": "更新时间",
    "defaultValue": {
      "$env": "now"
    }
  },
  "private": {
    "rules": [
      {
        "format": "bool"
      }
    ],
    "label": "私有",
    "title": "私有",
    "defaultValue": false
  },
  "is_delete": {
    "rules": [
      {
        "format": "bool"
      }
    ],
    "label": "是否删除",
    "title": "是否删除",
    "defaultValue": false
  },
  "sort": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "title": "排序",
    "label": "排序"
  },
  "user_id": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "label": "创建用户",
    "title": "创建用户"
  },
  "create_time": {
    "rules": [
      {
        "format": "timestamp"
      }
    ],
    "label": "创建时间",
    "title": "创建时间"
  }
}

const enumConverter = {}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
